﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LogTracerListenerPlugin.cs" company="">
// </copyright>
// <summary>
//   The log tracer listener plugin.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace LogTracerListener.SpecflowPlugin
{
    using System;

    using TechTalk.SpecFlow.Plugins;
    using TechTalk.SpecFlow.Tracing;

    using UITest.Report;

    /// <summary>
    ///     The log tracer listener plugin.
    /// </summary>
    public class LogTracerListenerPlugin : IRuntimePlugin
    {
        #region Public Methods and Operators

        /// <summary>
        /// The initialize.
        /// </summary>
        /// <param name="runtimePluginEvents">
        /// The runtime plugin events.
        /// </param>
        /// <param name="runtimePluginParameters">
        /// The runtime plugin parameters.
        /// </param>
        public void Initialize(RuntimePluginEvents runtimePluginEvents, RuntimePluginParameters runtimePluginParameters)
        {
            runtimePluginEvents.CustomizeGlobalDependencies += this.RuntimePluginEventsOnCustomizeGlobalDependencies;
        }

        #endregion

        #region Methods

        private void RuntimePluginEventsOnCustomizeGlobalDependencies(
            object sender, 
            CustomizeGlobalDependenciesEventArgs customizeGlobalDependenciesEventArgs)
        {
            customizeGlobalDependenciesEventArgs.ObjectContainer.RegisterTypeAs<LogTracerListener, ITraceListener>();
        }

        #endregion
    }

    /// <summary>
    ///     The log tracer listener.
    /// </summary>
    public class LogTracerListener : ITraceListener
    {
        #region Fields

        /// <summary>
        ///     The report.
        /// </summary>
        public ReportListener Report;

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The write test output.
        /// </summary>
        /// <param name="message">
        /// The message.
        /// </param>
        public void WriteTestOutput(string message)
        {
            Console.WriteLine(message);

            // Custom Message
            this.Report = ReportFactory.GetReportListener();
            this.Report.WriteTestOutput("Custom : " + message);
        }

        /// <summary>
        /// The write tool output.
        /// </summary>
        /// <param name="message">
        /// The message.
        /// </param>
        public void WriteToolOutput(string message)
        {
            Console.WriteLine("-> " + message);

            // Custom Message
            this.Report = ReportFactory.GetReportListener();
            if (message.Contains("error:"))
            {
                this.Report.WriteTestOutput("Custom : " + message);
            }
        }

        #endregion
    }
}